//package com.atguigu.eduservice.service.impl;
//
//import com.atguigu.eduservice.entity.EduCourse;
//import com.atguigu.eduservice.entity.EduCourseDescription;
//import com.atguigu.eduservice.entity.vo.CourseInfoVo;
//import com.atguigu.eduservice.mapper.EduCourseMapper;
//import com.atguigu.eduservice.service.EduCourseDescriptionService;
//import com.atguigu.eduservice.service.EduCourseService;
//import com.atguigu.servicebase.exceptionhandler.GuliException;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
///**
// * <p>
// * 课程 服务实现类
// * </p>
// *
// * @author testjava
// * @since 2021-10-10
// */
//@Service
//public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
//   @Autowired
//    private EduCourseDescriptionService courseDescriptionService;
//    //添加课程信息的基本方法
//    @Override
//    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
//        //向课程表添加数据
//        EduCourse eduCourse = new EduCourse();
//        //数据迁移
//        BeanUtils.copyProperties(courseInfoVo,eduCourse);
//        //返回的是添加的数据行数
//        int insert = baseMapper.insert(eduCourse);
//        if (insert==0){
//            throw new GuliException(20001,"添加课程信息失败");
//        }
//        //如果成功之后，获取对应的id
//        String cid = eduCourse.getId();
//        //向简介表添加数据
//        //另外一种添加数据的方法
//        EduCourseDescription courseDescription = new EduCourseDescription();
//        courseDescription.setDescription(courseInfoVo.getDescription());
//        //设置课程id与简介id一致
//        courseDescription.setId(cid);
//        courseDescriptionService.save(courseDescription);
//        //返回课程id
//        return cid;
//    }
//    //根据id查询课程基本信息
//    @Override
//    public CourseInfoVo getCourseInfo(String courseId) {
//        //查询课程表
//        EduCourse eduCourse = baseMapper.selectById(courseId);
//        CourseInfoVo courseInfoVo = new CourseInfoVo();
//        BeanUtils.copyProperties(eduCourse,courseInfoVo);
//        //查询简介表
//        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
//        courseInfoVo.setDescription(courseDescription.getDescription());
//
//        return courseInfoVo;
//    }
//    //修改课程信息
//    @Override
//    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
//        //修改课程信息
//        EduCourse eduCourse = new EduCourse();
//        BeanUtils.copyProperties(courseInfoVo,eduCourse);
//        int update = baseMapper.updateById(eduCourse);
//        if (update==0){
//            new GuliException(20001,"修改课程信息失败！");
//        }
//        //修改描述表
//        EduCourseDescription description = new EduCourseDescription();
//        description.setId(courseInfoVo.getId());
//        description.setDescription(courseInfoVo.getDescription());
//        courseDescriptionService.updateById(description);
//    }
//
//}
package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
//import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-03-02
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {


    //课程描述注入
   @Autowired
    private EduCourseDescriptionService courseDescriptionService;
   //小节和章节service
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService chapterService;


    //添加课程基本信息的方法
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1 向课程表添加课程基本信息
        //CourseInfoVo对象转换eduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert == 0) {
            //添加失败
            throw new GuliException(20001,"添加课程信息失败");
        }

        //获取添加之后课程id
        String cid = eduCourse.getId();

        //2 向课程简介表添加课程简介
        //edu_course_description
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置描述id就是课程id
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);

        return cid;
    }

    //根据课程id查询课程基本信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //2 查询描述表
        //2 查询描述表


        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
//        System.out.println(courseDescription);
        courseInfoVo.setDescription(courseDescription.getDescription());



        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1 修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0) {
            throw new GuliException(20001,"修改课程信息失败");
        }

        //2 修改描述表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(description);
    }
    //根据课程id查询课程确认的信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }
    //删除课程
    @Override
    public void removeCourse(String courseId) {
        //1.根据课程id删除小节
        eduVideoService.removeVideoByCourseId(courseId);
        //2.根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);
        //3.根据id删除描述
        //一对一的关系可以直接删除
        courseDescriptionService.removeById(courseId);
        //4.根据课程id删除课程本身
        int result = baseMapper.deleteById(courseId);
        if (result==0){
            throw new GuliException(20001,"删除失败！");
        }

    }
    //条件分页查询课程
    @Override
    public Map<String, Object> getCourseFrontlist(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        //根据id查询课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){//一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())){//二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){//关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){//最新
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())){//价格
            wrapper.orderByDesc("price");
        }
        baseMapper.selectPage(pageParam,wrapper);

        List<EduCourse> records = pageParam.getRecords();//每一页数据的集合
        long current = pageParam.getCurrent();//当前页数
        long pages = pageParam.getPages();//总页数
        long size = pageParam.getSize();//每页的记录数
        long total = pageParam.getTotal();//总的记录数
        boolean hasNext = pageParam.hasNext();//是否有下一页，有上一页
        boolean hasPrevious = pageParam.hasPrevious();//有上一页
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }
    //课程详情的方法
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {

        return baseMapper.getBaseCourseInfo(courseId);
    }
//
//    //根据课程id查询课程确认信息
//    @Override
//    public CoursePublishVo publishCourseInfo(String id) {
//        //调用mapper
//        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
//        return publishCourseInfo;
//    }

}
